package com.facebook.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Scoreboard extends Activity {
	
	private Facebook facebook;
	private static Map<String, String> friends;
	private static Map<String, String> reversedMap;
	private static LinkedList<NameValuePair> scores;
	private static String myID;
	
	public void onCreate(Bundle savedInstanceState) {
	     super.onCreate(savedInstanceState);
	     
	     // Getting the Bundle with the information from the Intent
	     Bundle stuff = this.getIntent().getExtras();
	     // Getting the APP_ID
	     String app_id = stuff.getString("api");
	     String text = stuff.getString("friends");
	     myID = stuff.getString("myID");
	     facebook = new Facebook(app_id);
	     // Restoring the session by obtaining the session key
	     SessionStore.restore(facebook, this);  
	     
	     // Set the scores LinkedList
	     getScores(text);
	     
	     TextView tv = new TextView(this);
	     if(scores.size() == 0){
	    	 tv.setText("None of your friends uses PennQuiz. Invite them!");
	     }
	     
	     else {
	    	 NameValuePair me = scores.removeLast();
	    	 // Use StringBuffer to save space on the stack
	    	 StringBuffer buff = new StringBuffer();
	    	 buff.append("SCOREBOARD:\n\n");
	    	 // Format the output
	    	 for(Iterator<NameValuePair> it = scores.iterator(); it.hasNext(); ){
	    		 NameValuePair next = it.next();
	    		 String uid = next.getName();
	    		 String name = new String();
	    		 if(uid.equals(me.getName()))
	    			 name = "YOU";
	    		 else
	    			 name = friends.get(uid);
	    		 String score = next.getValue();
	    		 buff.append(name);
	    		 buff.append(": ");
	    		 buff.append(score);
	    		 buff.append(" pts\n");
	    	 }
	    	 buff.append("\nYou have: ");
	    	 buff.append(me.getValue());
	    	 buff.append(" points.\n");
	    	 tv.setText(buff.toString());
	     }
	     setContentView(tv);
	}
    

	
    private static void getScores(String result){
    	// Load the requested page converted to a string into a JSONObject.  
		JSONObject json;
		try {
			json = new JSONObject(result);
			
			// Make array of the suggestions  
			JSONArray suggestions = json.getJSONArray("data");
			friends = new TreeMap<String, String>();
			reversedMap = new TreeMap<String, String>();
	
			// Build the friends map.
			for(int i = 0; i < suggestions.length(); i++){
				JSONObject single = suggestions.getJSONObject(i);
				friends.put(single.getString("id"), single.getString("name"));
				reversedMap.put(single.getString("name"), single.getString("id"));
			}

			// Format the ids string according to MySQL convention
			String ids = friends.keySet().toString();
			ids = ids.substring(1, ids.length()-1).replace(" ","");
			ids = ids + "," + myID;
			
			String domain = "http://www.pennquiz.com/users.php";
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("uid",ids));
			nameValuePairs.add(new BasicNameValuePair("funct","scoreboard"));
			RequestPoster rp = new RequestPoster(domain, nameValuePairs);
			
			// Set the scores map
			scores = rp.getScoreboard();
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
